package com.nativetalk.service.member.update;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.TdNtMemberPsd;
import com.nativetalk.bean.member.TdNtMemberRetrievePsd;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.common.util.MD5;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtMemberDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 15/12/27.
 */
@Service
public class MemberUpdateServiceImpl implements MemberUpdateService {
    private static Logger logger = Logger.getLogger(MemberUpdateServiceImpl.class);
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Override
    @Transactional
    public RetInfo updateTdNtMember(TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":updateTdNtMember:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tdNtMemberDao.updateTdNtMemberData(tdNtMember);

            MemcachedUtils.replace(tdNtMember.getToken(),tdNtMember, 30*24*60*60);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("更新用户资料成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    @Transactional
    public RetInfo updateTdNtMemberPsd(TdNtMemberPsd tdNtMemberPsd, TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":updateTdNtMember:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            String client_old_pwd = MD5.code(tdNtMemberPsd.getOld_memb_password());
            String old_pwd = tdNtMember.getMemb_password();
            String client_new_pwd = MD5.code(tdNtMemberPsd.getMemb_password());

            // 原始密码不相等
            if (!old_pwd.equals(client_old_pwd)) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("MEMBER.PASSWORD.ERROR"));
            } else if (old_pwd.equals(client_new_pwd)) {// 新密码和原始密码相同
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("新密码不能与旧密码一致");
            } else {
                TdNtMemberRetrievePsd tdNtMemberRetrievePsd = new TdNtMemberRetrievePsd();
                tdNtMemberRetrievePsd.setMemb_password(MD5.code(tdNtMemberPsd.getMemb_password()));
                tdNtMemberRetrievePsd.setToken(CommonUtil.getToken());
                tdNtMemberRetrievePsd.setMemb_id(tdNtMember.getMemb_id());
                /**
                 * 删除原缓存
                 */
                MemcachedUtils.delete(tdNtMember.getToken());
                /**
                 * 更新用户  放入缓存
                 */
                tdNtMember.setMemb_password(MD5.code(tdNtMemberPsd.getMemb_password()));
                tdNtMember.setToken(tdNtMemberRetrievePsd.getToken());
                MemcachedUtils.add(tdNtMemberRetrievePsd.getToken(),tdNtMember, 20*24*60*60);

                tdNtMemberDao.updateTdNtMemberRetrievePsd(tdNtMemberRetrievePsd);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("更新用户密码成功");
                Map<String,Object> map = new HashMap<>();
                map.put("token",tdNtMemberRetrievePsd.getToken());
                retInfo.setObj(map);
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public RetInfo updatevideoswitch(TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":updatevideoswitch:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tdNtMemberDao.updatevideoswitch(tdNtMember);
            MemcachedUtils.replace(tdNtMember.getToken(),tdNtMember, 30*24*60*60);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("更新用户资料成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    public void updateMemberJustTalkUserUri(Integer memb_id, String justTalkUserUri) {
        tdNtMemberDao.updateMemberJustTalkUserUri(memb_id, justTalkUserUri);
    }
}
